package com.julis.fragment;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.customview.InputViewOnlyShow;
import com.julis.R;
import com.julis.bean.FanHui;
import com.julis.bean.MyEvent;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import de.greenrobot.event.EventBus;



/**
 * 首页
 */
public class CarInfo2_Fragment extends Fragment {
    View view;
    @BindView(R.id.ipv_carno)
    InputViewOnlyShow ipvCarno;
    @BindView(R.id.ipv_carcolor)
    InputViewOnlyShow ipvCarcolor;
    @BindView(R.id.ipv_car_type)
    InputViewOnlyShow ipvCarType;
    @BindView(R.id.ipv_user_type)
    InputViewOnlyShow ipvUserType;
    @BindView(R.id.ipv_car_size)
    InputViewOnlyShow ipvCarSize;
    @BindView(R.id.ipv_wheel_count)
    InputViewOnlyShow ipvWheelCount;
    @BindView(R.id.ipv_axle_count)
    InputViewOnlyShow ipvAxleCount;
    @BindView(R.id.ipv_axle_distance)
    InputViewOnlyShow ipvAxleDistance;
    @BindView(R.id.ipv_approved_count)
    InputViewOnlyShow ipvApprovedCount;
    @BindView(R.id.ll_normal_car)
    LinearLayout llNormalCar;
    @BindView(R.id.ipv_character_des)
    InputViewOnlyShow ipvCharacterDes;
    @BindView(R.id.ipv_vin_trackercar)
    InputViewOnlyShow ipvVinTrackercar;
    @BindView(R.id.ll_truck_car)
    LinearLayout llTruckCar;
    Unbinder unbinder;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        if (view == null) {
            view = View.inflate(getActivity(), R.layout.fragment_carinfo, null);
            ButterKnife.bind(this, view);
            EventBus.getDefault().register(this);
        }
        ViewGroup parent = (ViewGroup) view.getParent();
        if (parent != null) {
            parent.removeView(view);
        }


        unbinder = ButterKnife.bind(this, view);
        return view;
    }
    //数据分发

    public void onEventMainThread(MyEvent event) {
        String code = event.getCode();
        if (code.equals("1")) {
            FanHui fanHui = event.getFanHui();
            Bundle bundle = fanHui.getBundle();
            initViewShow(bundle);
        } else if (code.equals("3")) {
            FanHui fanHui = event.getFanHui();
            Bundle bundle = fanHui.getBundle();
            initViewShow(bundle);
        } else {
            ipvCarno.setEditText("");
            ipvCarcolor.setEditText("");
            ipvCarType.setEditText("");
            ipvUserType.setEditText("");
            ipvCarSize.setEditText("");
            ipvWheelCount.setEditText("");
            ipvAxleCount.setEditText("");
            ipvAxleDistance.setEditText("");
            ipvApprovedCount.setEditText("");

        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    private void initViewShow(Bundle bundle) {
        ipvCarno.setEditText(bundle.getString("VLP"));
        ipvCarcolor.setEditText(bundle.getString("VLPC"));
        ipvCarType.setEditText(bundle.getString("VCLASS"));
        ipvUserType.setEditText(bundle.getString("VUSERTYPE"));
        ipvCarSize.setEditText(bundle.getString("VSIZE") + "-" + bundle.getString("VSIZE2"));
        ipvWheelCount.setEditText(bundle.getString("VWHEEL"));
        ipvAxleCount.setEditText(bundle.getString("VAXIS"));
        ipvAxleDistance.setEditText(bundle.getString("VAXISWIDTH"));
        ipvApprovedCount.setEditText(bundle.getString("VWeight"));
        ipvCharacterDes.setEditText(bundle.getString("VDESCIBE"));
        ipvVinTrackercar.setEditText(bundle.getString("VENGINESN"));
    }

}
